/*
 * Board.java
 *
 * Created on 05. Juni 2007, 02:44
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sbc.repos;

import sbc.util.*;
import java.util.*;
import org.xvsm.api.core.*;
import corso.lang.*;

/**
 *
 * @author mustermark
 */
public class Unit extends Module {
    
    //private Sensor sensorOne, sensorTwo;
    //private Board board;
    private Module.Type type = Module.Type.UNIT;
    private String uid;
    private CorsoVarOid boardOid, sensorOneOid, sensorTwoOid;
    private Board board;
    private Sensor sensorOne, sensorTwo;
    
    /** Creates a new instance of Board */
    public Unit(CorsoVarOid oid) {
        super(oid);
    }
    public Unit() {
        boardOid = new CorsoVarOid ();
        sensorOneOid = new CorsoVarOid ();
        sensorTwoOid = new CorsoVarOid ();
    }
    
    public Unit(Board b, Sensor s1, Sensor s2) {
        uid = b.getUID() + ":" + s1.getUID() + ":" + s2.getUID();
        board = b;
        sensorOne = s1;
        sensorTwo = s2;
    }
    
    public String getUID() {
        return uid;
    }
    public Type getType() { return type; }
    
    public Tuple asTuple() {
        Tuple tuple = new Tuple();
        tuple.addEntry(Entry.Factory.newInstance(ValueTypes.STRING_UTF8, type.toString()));
        tuple.addEntry(Entry.Factory.newInstance(ValueTypes.STRING_UTF8, uid));
        //for(Feature f : features) tuple.addEntry(Entry.Factory.newInstance(ValueTypes.STRING_UTF8, f.getLabel()));
        return tuple;
    }
    
     public void write (CorsoData data) throws CorsoDataException {
        data.putStructTag (type.toString(), 3);
        data.putShareable(boardOid);
        data.putShareable(sensorOneOid);
        data.putShareable(sensorTwoOid);
        
    }
    public void read (CorsoData data) throws CorsoDataException {
        StringBuffer structName = new StringBuffer ();
        int arity = data.getStructTag (structName);
        if (arity != 3 || !structName.toString ().equals (getType().toString()))
            throw new CorsoDataException ();
        
        data.getShareable(boardOid);
        data.getShareable(sensorOneOid);
        data.getShareable(sensorTwoOid);
        
    }
    
    
    
}

